﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeColorHtml;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using WebView.Models;
using WebView.Tool;

namespace WebView.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private IWebHostEnvironment _env;

        public HomeController(ILogger<HomeController> logger, IWebHostEnvironment env)
        {
            _logger = logger;
            _env = env;
        }

        public IActionResult Index()
        {
            string fileFullName = Path.Combine(GetTempDir(),"csharptemp.txt");
            string text = GetFileText(fileFullName);
            CodeColourHtml code = new CodeColourHtml();
            string htmlCode = code.Colour(text, CodeType.CSharp);
            ViewBag.Text = htmlCode;
            return View();
        }

        public IActionResult Shell()
        {
            string fileFullName = Path.Combine(GetTempDir(), "shelltemp.txt");
            string text = GetFileText(fileFullName);
            CodeColourHtml code = new CodeColourHtml();
            string htmlCode = code.Colour(text, CodeType.Shell);
            ViewBag.Text = htmlCode;
            return View();
        }

        public IActionResult Sql()
        {
            string fileFullName = Path.Combine(GetTempDir(), "sqltemp.txt");
            string text = GetFileText(fileFullName);
            CodeColourHtml code = new CodeColourHtml();
            string htmlCode = code.Colour(text, CodeType.Sql);
            ViewBag.Text = htmlCode;
            return View();
        }

        public IActionResult Javascript()
        {
            string fileFullName = Path.Combine(GetTempDir(), "javascripttemp.txt");
            string text = GetFileText(fileFullName);
            CodeColourHtml code = new CodeColourHtml();
            string htmlCode = code.Colour(text, CodeType.JavaScript);
            ViewBag.Text = htmlCode;
            return View();
        }


        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        private string GetFileText(string fileFullName)
        {
            string text = string.Empty;
            Encoding encode = TxtFileEncoding.GetEncoding(fileFullName);
            using (FileStream fileStream = new FileStream(fileFullName, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(fileStream, encode))
                {
                    String line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (string.IsNullOrEmpty(text))
                        {
                            text += line;
                        }
                        else
                        {
                            text += Environment.NewLine + line;
                        }
                    }
                }
            }
            return text;
        }

        private string GetTempDir()
        {
            string dir = GetRootMapPath("");
            if (_env.IsDevelopment())
            {
                dir = Path.GetFullPath("..\\..\\..", dir);
                dir = Path.Combine(dir, "wwwroot");
            }
            else
            {
                dir = Path.Combine(dir, "wwwroot");
            }
            return Path.Combine(dir, "temp");
        }


        private string GetRootMapPath(string strPath)
        {
            string rootDir = AppContext.BaseDirectory;
            string dir = string.Empty;
            if (!string.IsNullOrEmpty(strPath))
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart(new char[]
                    {
                    '\\'
                    });
                }
                dir = Path.Combine(rootDir, strPath);
            }
            else
            {
                dir = rootDir;
            }

            return dir;
        }
    }
}
